﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tao.FreeGlut;
using Tao.OpenGl;

namespace OpenGl
{
    static public class Drawer
    {
        private static float[] DrawColor= new float[3]{1,1,1};
        private static float[] BackgroundColor = new float[4]{0,0,0,0};
        private static void PutVertex(float x, float y, float z)
        {
            Gl.glVertex3f(x,y,z);
        }

        private static void SetNowColor()
        {
            Gl.glColor3f(DrawColor[0], DrawColor[1], DrawColor[2]);
        }

        public static void FlushAndSwapBuffers()
        {
            Gl.glFlush();
            Glut.glutSwapBuffers();
        }
        public static void Flush()
        {
            Gl.glFlush();
        }
        public static void SwapBuffers()
        {
            Glut.glutSwapBuffers();
        }
        public static void Refresh()
        {
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
            Gl.glLoadIdentity();
            FlushAndSwapBuffers();
        }

        public static void SetColor3f(float R, float G, float B)
        {
            DrawColor[0] = R;
            DrawColor[1] = G;
            DrawColor[2] = B;
            Gl.glColor3f(R,G,B);
        }
        public static void SetColor3i(int R, int G, int B)
        {
            float RR = R/255;
            float GG = G/255;
            float BB = B/255;
            SetColor3f(RR,GG,BB);
        }
        public static float[] GetColor3f()
        {
            return DrawColor;
        }
        public static int[] GetColor3i()
        {
            int[] color = new int[3];
            color[0] = (int)(DrawColor[0]*255);
            color[1] = (int) (DrawColor[1]*255);
            color[2] = (int) (DrawColor[2]*255);
            return color;
        }

        public static void SetBackgroundColor3f(float R, float G, float B, float A)
        {
            BackgroundColor[0] = R;
            BackgroundColor[1] = G;
            BackgroundColor[2] = B;
            BackgroundColor[3] = A;
            Gl.glClearColor(R, G, B,A);
        }
        public static void SetBackgroundColor3i(int R, int G, int B, int A)
        {
            float RR = R/255;
            float GG = G/255;
            float BB = B/255;
            float AA = A/255;
            SetBackgroundColor3f(RR,GG,BB,AA);
        }
        public static float[] GetBackgroundColor3f()
        {
            return BackgroundColor;
        }

        public static int[] GetBackgroundColor3i()
        {
            int[] color = new int[3];
            color[0] = (int) (BackgroundColor[0]*255);
            color[1] = (int) (BackgroundColor[1]*255);
            color[2] = (int) (BackgroundColor[2]*255);
            return color;
        }

        public static void DrawRectangle(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3, float x4, float y4, float z4)
        {
            Gl.glBegin(Gl.GL_POLYGON);
            SetNowColor();
            PutVertex(x1,y1,z1);
            PutVertex(x2, y2, z2);
            PutVertex(x3,y3,z3);
            PutVertex(x4,y4,z4);
            Gl.glEnd();
        }
    }
}
